home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-09-10 | 13.9 KB | 185 lines | [TEXT/MPCC] |
- /*---------------------------------------------------------------
- Copyright 1995, Steve Israelson
-
- I own this code. You are free to use this code in any software
- you want. You may not sell this source code at all, you can
- sell your product though. If you want to include this code
- in any code collection (CD-Roms etc) this is OK as long as
- I get a complimentary copy.
- Steve.
-
- Uses a parser to parse one line of a script. Handles all the
- needed flow control constructs. The caller must be able
- to jump to any line in the script specified.
-
- The flow control works as follows:
-
- If (expr) // this evaluates the expr and executes code if true, until and Endif or Else
- Else // if the expr is false in the If() statement, then the code between the Else and Endif executes
- Endif // terminates the If() statement
- While (expr)// while the expr is true, the code between the While() and EndWhile executes
- EndWhile // loops to the previous while statement
- Repeat // starts a repeat loop. Code executes until the Until() statement is true
- Until(expr) // loops to the previous Repeat statement unless the expr is true
- Gosub label // Code execution continues at the matching Subroutine statement
- Subroutine label // Indicates the start of a subroutine and its label. Code only executes in response to Gosub
- EndSub // Execution resumes on the line following the Gosub that called us.
-
- And the expressions (expr above):
- num < num
- num <= num
- num = num
- num != num
- num >= num
- num > num
- Where num can be anything your numerical parser can parse into an integer.
-
- ---------------------------------------------------------------*/
- #include "StevesFlowControl.h"
- #include <String.h>
-
- /*---------------------------------------------------------------
- Build our two parsers.
- Pass in a pointer to your numerical evaluation function.
- Pass in any data you want for itsData. It will be passed
- to your numerical evaluator.
- ---------------------------------------------------------------*/
- MyFlowControl::MyFlowControl(numerical mp;
- }
- theParams.RemoveItemsAt(1000, 1);
-
- if (token > kToken_Error_Last) // only return errors and stuff
- token = kToken_Ignore;
- return token;
- }
-
- /*---------------------------------------------------------------
- Find and return a subroutine entry in our list.
- Checks each one and returns the correct one or nil.
- ---------------------------------------------------------------*/
- subroutineEntry *MyFlowControl::FindSubroutine(char *text)
- {
- for (int x = 1; x <= Subroutines.GetCount(); ++x)
- {
- subroutineEntry *theEntry;
- if (Subroutines.FetchItemAt(x, &theEntry))
- if (!strcmp(text, theEntry->itsName))
- return theEntry;
- }
- return nil;
- }
-
- /*---------------------------------------------------------------
- Add a number to the param list. Should use atoi(), but where
- is it?
- ---------------------------------------------------------------*/
- void MyFlowControl::AddLongParam(LList *theParams, long theNum)
- {
- char *theStr = NewPtr(64);
- NumToString(theNum, (StringPtr)theStr);
- PtoCstr((StringPtr)theStr);
- theParams->InsertItemsAt(1, 1, &theStr);
- }
-
- /*---------------------------------------------------------------
- Add an item to the stack. The condition state is for things
- like if statements. Its true if the condition is true.
- canExecute is false if the code can not execute any way, usually
- because we are inside a nested if that is not executing.
- ---------------------------------------------------------------*/
- void MyFlowControl::PushOntoStack(long token, long theLineNum, char *theLabel,
- Boolean conditionState, Boolean canExecute, Boolean isSearching)
- {
- stackEntry *newEntry = (stackEntry*)NewPtrClear(sizeof(stackEntry));
- if (newEntry)
- {
- newEntry->token = token;
- newEntry->itsLineNum = theLineNum;
- newEntry->theLabel = theLabel;
- newEntry->isExecuting = canExecute;
- newEntry->isSearching = isSearching;
- newEntry->conditionState = conditionState;
-
- Stack.InsertItemsAt(1, arrayIndex_Last, &newEntry);
- }
- else // error?
- if (theLabel)
- DisposePtr(theLabel); // clean up
- }
-
- /*---------------------------------------------------------------
- Toast the top item on the stack
- ---------------------------------------------------------------*/
- void MyFlowControl::PopStack(void)
- {
- stackEntry *oldEntry;
- if (Stack.FetchItemAt(arrayIndex_Last, &oldEntry))
- {
- Stack.Remove(&oldEntry);
- if (oldEntry->theLabel)
- DisposePtr(oldEntry->theLabel);
- DisposePtr((Ptr)oldEntry);
- }
- }
-
- /*---------------------------------------------------------------
- Evaluate the expression and return its truth.
- Calls your code to find the numerical value of
- a numerical expression, which could include variables.
- ---------------------------------------------------------------*/
- Boolean MyFlowControl::EvalExpression(char *theExpression)
- {
- Boolean returnValue = false;
- long token;
- LList theParams;
- char *paramater1, *paramater2;
- long n1, n2;
-
- // parse the given line and get the token
- token = expressions.Parse(theExpression, &theParams);
-
- // get the two parameters (should always be two too
- theParams.FetchItemAt(1, ¶mater1);
- theParams.FetchItemAt(2, ¶mater2);
-
- n1 = (*evalNumber)(paramater1, theUserData);
- n2 = (*evalNumber)(paramater2, theUserData);
-
- switch (token)
- {
- default: // expression error
- break;
- case kToken_Equal:
- returnValue = (n1 == n2);
- break;
- case kToken_GreaterThan:
- returnValue = (n1 > n2);
- break;
- case kToken_LessThan:
- returnValue = (n1 < n2);
- break;
- case kToken_NotEqual:
- returnValue = (n1 != n2);
- break;
- case kToken_GreaterThanOrEqual:
- returnValue = (n1 >= n2);
- break;
- case kToken_LessThanOrEqual:
- returnValue = (n1 <= n2);
- break;
- }
-
- // toast all the params
- for (int i = 1; i < theParams.GetCount(); ++i)
- {
- char *temp;
- if (theParams.FetchItemAt(i, &temp))
- delete temp;
- }
- theParams.RemoveItemsAt(1000, 1);
- return returnValue;
- }
-
-
-
-